package com.dify.knowledge.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dify.knowledge.entity.AnswerRating;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;

/**
 * 解答评分Mapper接口
 */
@Mapper
public interface AnswerRatingMapper extends BaseMapper<AnswerRating> {
    
    /**
     * 检查用户在指定日期是否已对某答案评分
     * @param answerId 答案ID
     * @param userId 用户ID
     * @param date 评分日期
     * @return 是否存在评分记录
     */
    @Select("SELECT COUNT(*) > 0 FROM answer_rating " +
            "WHERE manual_answer_id = #{answerId} AND user_id = #{userId} AND rating_date = #{date}")
    boolean existsByAnswerIdAndUserIdAndDate(@Param("answerId") Long answerId, 
                                              @Param("userId") Long userId, 
                                              @Param("date") LocalDate date);
    
    /**
     * 统计某答案的有用评分数
     * @param answerId 答案ID
     * @return 有用评分数
     */
    @Select("SELECT COUNT(*) FROM answer_rating WHERE manual_answer_id = #{answerId} AND is_useful = true")
    int countUsefulRatings(@Param("answerId") Long answerId);
    
    /**
     * 统计某答案的无用评分数
     * @param answerId 答案ID
     * @return 无用评分数
     */
    @Select("SELECT COUNT(*) FROM answer_rating WHERE manual_answer_id = #{answerId} AND is_useful = false")
    int countUselessRatings(@Param("answerId") Long answerId);
}
